package com.genokon.druid.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.genokon.druid.enums.DataSourceEnum;
import com.genokon.druid.multiple.MultipleDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author by baichaoda
 * @date 2020/7/31 10:37
 */
@Configuration
@MapperScan("com.genokon.druid.mapper*")
public class MybatisPlusConfiguration {

    @Resource
    private MetaHandler metaHandler;

    // @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }

    //@Bean
    public PageInterceptor pageInterceptor() {
        return new PageInterceptor();
    }

    //@Bean(name = "dbKnowledge")
   // @ConfigurationProperties(prefix = "spring.datasource.druid.db-klg" )
    public DataSource db1() {
        return DruidDataSourceBuilder.create().build();
    }

    //@Bean(name = "dbTmp")
   // @ConfigurationProperties(prefix = "spring.datasource.druid.db-tmp" )
    public DataSource db2() {
        return DruidDataSourceBuilder.create().build();
    }

    /**
     * ��̬����Դ����
     * @return
     */
    //@Bean
    //@Primary
    public DataSource multipleDataSource(@Qualifier("dbKnowledge") DataSource db1, @Qualifier("dbTmp") DataSource db2) {
        MultipleDataSource multipleDataSource = new MultipleDataSource();
        Map< Object, Object > targetDataSources = new HashMap<>(1);
        targetDataSources.put(DataSourceEnum.dbKlg.getValue(), db1);
        targetDataSources.put(DataSourceEnum.dbGeneService.getValue(), db2);
        multipleDataSource.setTargetDataSources(targetDataSources);
        multipleDataSource.setDefaultTargetDataSource(db1);
        return multipleDataSource;
    }

    //@Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(multipleDataSource(db1(),db2()));

        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCacheEnabled(false);
        GlobalConfig globalConfig = GlobalConfigUtils.defaults();
        globalConfig.setMetaObjectHandler(metaHandler);
        globalConfig.getDbConfig().setIdType(IdType.ASSIGN_UUID);
        sqlSessionFactory.setConfiguration(configuration);
        sqlSessionFactory.setGlobalConfig(globalConfig);
        sqlSessionFactory.setPlugins(//PerformanceInterceptor(),OptimisticLockerInterceptor()
                paginationInterceptor());
        return sqlSessionFactory.getObject();
    }
}
